package com.dinadi.yidi.ui.activity;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.MyActivitysAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.MessageBean;
import com.dinadi.yidi.bean.MyActivitysRequest;
import com.dinadi.yidi.bean.MyActivitysRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/6/26  我的活动
 * Time: 13:31
 */
public class MyActivitysActivity extends BaseActivity {
    private View view;
    private TextView tvTitle;
    private RelativeLayout layout_no_comment;
    private ImageView img_no_comment;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    private MyActivitysAdapter mRefreshAdapter;
    private List<MyActivitysRequestEntity> dataList = new ArrayList<>();
    private String userId, token;
    private int page = 1;
    /*是否显示没有的图片*/
    private boolean isShow = true;


    @Override
    protected View getLayout() {
        view = LayoutInflater.from(MyActivitysActivity.this).inflate(R.layout.activity_message, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText("我的活动");
        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        //显示下拉刷新
        //   mPullLoadMoreRecyclerView.setRefreshing(true);
        mPullLoadMoreRecyclerView.setLinearLayout();
        mRefreshAdapter = new MyActivitysAdapter(MyActivitysActivity.this);
        mPullLoadMoreRecyclerView.setAdapter(mRefreshAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());
        layout_no_comment = (RelativeLayout) view.findViewById(R.id.layout_no_comment);
        img_no_comment = (ImageView) view.findViewById(R.id.img_no_comment);

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        userId = SharePreferencesHelper.getInstance(this).getString("userId");
        token = SharePreferencesHelper.getInstance(this).getString("token");
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        initPullrefresh();

    }

    @Override
    protected void clickEvent(View v) {

    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                MyActivitysRequest bean = JsonUtil.parseJsonToBean(response, MyActivitysRequest.class);
                dataList = bean.getBloodList();
                if (dataList != null && dataList.size() != 0) {
                    mRefreshAdapter.addAllData(dataList);
                    isShow = false;
                } else {
                    if (isShow == false) {
                        layout_no_comment.setVisibility(View.GONE);
                    } else {
                        layout_no_comment.setVisibility(View.VISIBLE);
                        img_no_comment.setImageResource(R.drawable.activity_lllustration_activity);
                    }
                }
                getData();
            } else {
                closeLoadingDialog();
                toast(errMsg);
            }

        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }

    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
        }

        @Override
        public void onLoadMore() {
            page = page + 1;
            initPullrefresh();
        }
    }

    private void setRefresh() {
        mRefreshAdapter.clearData();
        page = 1;
        initPullrefresh();
    }

    private void initPullrefresh() {
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        bean.setUserId(userId);
        bean.setPageNum(page + "");
        bean.setPageSize("10");
        CommonInterface messageInterface = new CommonInterface(MyActivitysActivity.this, httpListener);
        messageInterface.getInterface("12007", JSON.toJSONString(bean));
    }
}
